package com.wuwei.tms.workbench.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.tms.exception.TmsException;
import com.wuwei.tms.pojo.User;
import com.wuwei.tms.setting.mapper.UserMapper;
import com.wuwei.tms.utils.MyUtils;
import com.wuwei.tms.vo.BorrowPrint;
import com.wuwei.tms.vo.HistoryPrint;
import com.wuwei.tms.workbench.service.BorrowService;
import com.wuwei.tms.workbench.service.HistoryService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RequestMapping("T01/print")
@Controller
public class PrintController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private BorrowService borrowService;

    @GetMapping("/printBorrow/{empno}")
    public ModelAndView  printBorrow(@PathVariable("empno") String empno, ModelAndView model){
        Long value = Long.valueOf(empno);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmpno,empno);
        User user = userMapper.selectOne(wrapper);
        if (user==null) {
            throw new TmsException(30011,"员工号错误！");
        }
        List<BorrowPrint> returnList = borrowService.printForList(value);
        if (returnList.size()==0) {
            throw new TmsException(30011,"查询结果为空时不支持打印！");
        }
        model.setViewName("borrow");
        model.addObject("name",user.getName());
        model.addObject("list",returnList);
        return model;
    }

    @Resource
    private HistoryService historyService;


    @GetMapping("/printHistory")
    public ModelAndView printHistory(@RequestParam Map<String, String> params, ModelAndView model){
        LocalDateTime borrowDate1 = MyUtils.myDateFormatt((String)params.get("borrowTime1"));
        LocalDateTime borrowDate2 = MyUtils.myDateFormatt((String)params.get("borrowTime2"));
        LocalDateTime returnDate1 = MyUtils.myDateFormatt((String)params.get("returnTime1"));
        LocalDateTime returnDate2 = MyUtils.myDateFormatt((String)params.get("returnTime2"));
        long borrowById = Long.parseLong((String)params.get("borrowById"));
        long barcode = Long.parseLong((String)params.get("barcode"));
        String airplaneId = params.get("airplaneId");
        List<HistoryPrint> histories = this.historyService.listAll(borrowDate1,borrowDate2, returnDate1,returnDate2, borrowById, barcode, airplaneId);
        if (histories.size()==0) {
            throw new TmsException(30011,"查询结果为空时不支持打印！");
        }
        model.setViewName("history");
        model.addObject("list",histories);
        return model;
    }
}
